package net.jlibrary.example.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.example.action.vo.BookAdminActionVo;
import net.jlibrary.example.service.IBookManager;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/example")
public class BookAdminAction {
	@Resource
	private IBookManager bookManager;
	
	@RequestMapping(value = "/bookAdminView", method = {RequestMethod.GET})
	public ModelAndView view() {
		return new ModelAndView("/example/bookAdminView");
	}
	@RequestMapping(value = "/bookAdminJsonList", method = {RequestMethod.POST})
	public Pagination lists(BookAdminActionVo book ,PaginationParam param) {
		Pagination pagination = bookManager.listsPage(book, param);
		return pagination;
	}
	@RequestMapping(value = "/bookAdminJsonDetail", method = {RequestMethod.GET})
	public BookAdminActionVo detail(BookAdminActionVo book) {
		book =  bookManager.get(book);
		return book;
	}
	@RequestMapping(value = "/bookAdminJsonAddFormInit", method = {RequestMethod.GET})
	public BookAdminActionVo addFormInit() {
		BookAdminActionVo book = new BookAdminActionVo();
		return book;
	}
	@RequestMapping(value = "/bookAdminJsonSave", method = {RequestMethod.POST})
	@Token
	public ModelMap save(@ModelAttribute("vm") BookAdminActionVo book, @Token String token, BindingResult result) {
		bookManager.save(book);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}
	
	@RequestMapping(value = "/bookAdminJsonUpdate", method = {RequestMethod.POST})
	@Token
	public ModelMap update(@ModelAttribute("vm") BookAdminActionVo book, @Token String token, BindingResult result) {
		bookManager.update(book);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}
	
	@RequestMapping(value = "/bookAdminJsonDelete", method = {RequestMethod.POST})
	public ModelMap delete(Long id) {
		BookAdminActionVo book = new BookAdminActionVo();
		book.setId(id);
		bookManager.delete(book);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}
	
	
	@RequestMapping(value = "/bookAdminJsonData", method = {RequestMethod.GET})
	public List<ModelMap> bookAdminData() {
		List<BookAdminActionVo> books = bookManager.lists(new BookAdminActionVo());
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (BookAdminActionVo book : books) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, book.getPrimaryKey());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, book.getText());
			maps.add(map);
		}
		return maps;
	}
}
